define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Buyerpayment, GoPage, $stateParams, $ionicPopup, GoDialog, $ionicModal, FileSystem,$timeout) {
		
		$scope.order_id = $stateParams.order_id;//订单id
		$scope.tab = $stateParams.tab;//afall中的tab序号，事件处理完跳回到相应的tab
		$scope.input = new Object();
		$scope.is_order = true;
		$scope.is_pay = false;
		$scope.act_type = '';
		
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.init_data();
		}, false);
		
		//页面跳转
		$scope.onshow_afpay = function() {
			GoPage.go_states("afpay");
		}
		
		var myPopup;
		$scope.show_order_log = function() {
			$scope.is_order = true;
			$scope.is_pay = false;
			myPopup = $ionicPopup.show({ //show()
				templateUrl: 'templates/modal/order_log.html',
				title: "订单追踪",
				scope: $scope, //绑定数据，必须写这行
			});
		}
		
		var mPopup;
		$scope.show_pay_info = function() {
			$scope.is_order = false;
			$scope.is_pay = true;
			mPopup = $ionicPopup.show({ //show()
				templateUrl: 'templates/modal/pay_info.html',
				title: "支付信息",
				scope: $scope, //绑定数据，必须写这行
			});
		}
		
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {
			if(event.target.nodeName === 'HTML') {　　　　
				if(myPopup) {　　　　　　
					myPopup.close();　　　　
				}
				if(mPopup) {
					mPopup.close();
				}
				if(customPopup){
					customPopup.close();
				}　　　　　
			}
		})
		
		//初始化页面
		$scope.init_data = function() {
			Buyerpayment.get_order($scope.order_id, function(data) {
				console.log(data);
				if (!data) {
					GoDialog.alert("提示","对不起,找不到订单详情",function(){
						GoPage.go_back();
					});
					return;
				}
				$scope.order = data;
			 	$scope.showinvoicexia=false;  //是否显示展开图标
				//判断是否显示发票信息			
				if($scope.order.is_tax=="person"){  //只显示发表抬头 				
					$scope.showinvoicexia=false;										
				}
				if($scope.order.is_tax=="vat"){ //显示全部				
					$scope.showinvoicexia=true;										
				}
				if($scope.order.score_g) {
					$scope.order.score_g = parseInt(data.score_g);
				}
				$scope.gift_score = parseInt(data.score_u);
			});
		}
		
		/**	
		//待评价--4个按钮
		$scope.onshow_waitEvaluate = function(index) {
			if(index == 3) GoPage.go_states("aftalk");
		}
		//退款退货
		$scope.onshow_refund = function() {
			GoPage.go_states("refund");
		}
		**/
		
		//多个事件
		$scope.switchEvent = function(index, errorMsg, confirmMsg) {
			if(errorMsg){//有错误提示，就不触发以下事件
				GoDialog.alert("操作失败",errorMsg);
				return;
			}
			switch(index) {
				case 1://取消订单
					GoDialog.confirm("取消订单",confirmMsg,function(){
						$scope.cancel_order($scope.order.order_id);
					})
					break;
				case 2://确认收货
					GoDialog.confirm("确认收货",confirmMsg,function(){
						$scope.go_finish($scope.order.order_id);
					})
					break;
				case 3:
					$scope.delay_goods($scope.order.order_id);
					break;
				case 4:
					$scope.go_afback($scope.order.order_id);
					break;
				case 5:
					$scope.notice_seller($scope.order.order_id);
					break;
				case 6:
					$scope.go_afback($scope.order.order_id);
					break;
				case 7:
					$scope.go_afback($scope.order.order_id);
					break;
				case 8:
					$scope.go_afback($scope.order.order_id);
					break;
				case 9:
					$scope.go_afback($scope.order.order_id);
					break;
				case 10:
					$scope.go_afback($scope.order.order_id);
					break;
				case 11:
					$scope.go_aftalk($scope.order.order_id, false, $scope.order.order_type_id, $scope.order.act_id);
					break;
				case 12:
					$scope.go_aftalkagain($scope.order.order_id);
					break;
				case 13:
					$scope.go_pay($scope.order.order_id);
					break;
				case 14:
					$scope.doShowFile($scope.order.order_id, $scope.order.act_file_id, $scope.order.order_type_id);
					break;
				case 15:
					$scope.file_download($scope.order.act_file_id);
					break;
				case 16://删除订单
					GoDialog.confirm("删除订单",confirmMsg,function(){
						$scope.go_delete($scope.order.order_id);
					})
					break;
				case 17://查看物流
					GoPage.go_states("orderlogistics",{'order_id':$scope.order.order_id});
					break;	
			}
		}
		
		//取消订单
		$scope.cancel_order = function(order_id) {
			Buyerpayment.cancel_order(order_id, function(data) {
				if(data.success) {
					GoDialog.toast("取消订单成功");
					GoPage.go_states("afall",{'type':$scope.tab});
				} else{
					GoDialog.toast(data.msg);
				}
			})
		}
		
		//删除订单
		$scope.go_delete = function(order_id){
			Buyerpayment.go_delete(order_id, function(data) {
				if(data.success == true) {
					GoDialog.toast("确认订单成功");
					GoPage.go_states("afall",{'type':$scope.tab});
				} else{
					GoDialog.toast(data.msg);
				}
			})
		}
		
		//订单类型是gift不跳转
		$scope.onshow_first = function(store_id,order_type_id) {
			if(order_type_id=="gift"){
				return;
			}			
			GoPage.go_states("first", {
				"store_id": store_id
			});
		}
		
		//商品详情
		$scope.onshow_productdetail = function(goods_id, product_id, type, act_id) {
			if(type == 'newtest')
				GoPage.go_states("newtest_detail", {
					"act_id": act_id,
					"goods_id": goods_id
				});
			else if(type == 'newnotice')
				GoPage.go_states("newnotice_detail", {
					"act_id": act_id,
					"goods_id": goods_id
				});
			else if(type == 'newsend')
				GoPage.go_states("newsend_detail", {
					"act_id": act_id,
					"goods_id": goods_id
				});
			else if(type == 'groupbuy')
				GoPage.go_states("groupbuy_goods", {
					"act_id": act_id,
					"goods_id": goods_id,
					"product_id": product_id
				});
			else if(type == 'timedbuy')
				GoPage.go_states("panicbuying_goods", {
					"act_id": act_id,
					"goods_id": goods_id,
					"product_id": product_id
				});
			else if(type == 'spike')
				GoPage.go_states("seckill_goods", {
					"act_id": act_id,
					"goods_id": goods_id,
					"product_id": product_id
				});
			else if(type == 'gift') {
				GoPage.go_states("pointgoods", {
					"id": goods_id
				});
			} else
				GoPage.go_states("productdetail", {
					"goods_id": goods_id,
					'type': "goods"
				});
		};
	
		//确认收货,不要支付密码
		$scope.go_finish = function(order_id) {
//			$ionicPopup.show({ //show()
//				templateUrl: 'templates/modal/finish_input.html',
//				title: "确认收货",
//				scope: $scope, //绑定数据，必须写这行
//				buttons: [{
//					text: "取消",
//					type: "button-positive",
//					onTap: function() {
//						$scope.input.password = "";
//					}
//				}, {
//					text: "提交",
//					type: "button-assertive",
//					onTap: function() { // 当点击保存按钮时，执行该函数
//						if(!$scope.input.password) {
//							GoDialog.toast("请输入支付密码！");
//							e.preventDefault();
//						}
						Buyerpayment.go_finish(order_id,function(data) {
							if(data.success == true) {
								$scope.init_data();
								GoDialog.toast("确认收货成功");
							} else
								GoDialog.toast(data.msg);
						})
//					}
//				}]
//			});
		}
		
		//延长收货
		$scope.delay_goods = function(order_id) {
			Buyerpayment.go_extend(order_id, function(data) {
				if(data.success == true) {
					GoDialog.toast("延长收货成功");
				} else {
					GoDialog.toast(data.msg);
				}
			})
		}
		
		//提醒发货
		$scope.notice_seller = function(order_id, store_id) {
			Buyerpayment.go_remind(order_id, function(data) {
				if(data.success == true) {
					GoDialog.toast("提醒卖家成功");
				} else {
					GoDialog.toast(data.msg);
				}
			})
		}
		
		$scope.go_afback = function(order_id) {
			GoPage.go_states("afback", {
				"order_id": order_id
			});
		}
		
		$scope.go_aftalk = function(order_id, is_new, act_type, act_id) {
			GoPage.go_states("aftalk", {
				"order_id": order_id,
				"is_new": is_new,
				"act_type": act_type,
				"act_id": act_id
			});
		}
		$scope.go_aftalkagain = function(order_id) {
			GoPage.go_states("aftalkagain", {
				"id": order_id			
			});
		};
		
		$scope.go_pay = function(order_id) {
			var order = new Array();
			order[0] = order_id;
			order = JSON.stringify(order);
			GoPage.go_states("afpay", {
				"order_id": order
			});
		}
		$ionicModal.fromTemplateUrl('templates/modal/file_list.html', {
			scope: $scope,
		}).then(function(modal) {
			$scope.agentModal = modal;
		});
		$scope.$on('$destroy', function() {
			if($scope.agentModal)
				$scope.agentModal.remove();
		});
		$scope.filelist_close = function() {
			$scope.agentModal.hide();
		}
		$scope.doShowFile = function(act_file_id) {
			$scope.agentModal.show();
			$scope.initfile(act_file_id);
		}
		$scope.initfile = function(act_file_id) {
			//根目录
			FileSystem.readFileSystemRoot(act_file_id, function(data) {
				$scope.files = new Array();
				for(var i = 0; i < data.length; i++) {
					if(data[i].isFile)
						$scope.files.push(data[i]);
				}
			}, function(e) {
				GoDialog.alert('提示', '获取列表失败');
			});
		};
		//打开文件
		$scope.openfile = function(item) {
			var type = item.name.split('.');
			type = type[type.length - 1];
			if(type == 'doc' || type == 'docx') {
				type = 'application/msword';
			} else if(type == 'xls' || type == 'xlsx') {
				type = 'application/vnd.ms-excel';
			}
			FileSystem.fileOpenWithDialog(item.nativeURL, type, function() {
				GoDialog.alert('提示', '打开文件成功');
			}, function(e) {
				GoDialog.alert('提示', '打开文件失败');
			});
		};
		$scope.file_checked = function(index) {
			for(var i = 0; i < $scope.files.length; i++) {
				if(i != index) {
					$scope.files[i].check = false;
				}
			}
		}
		$scope.doUploadFile = function(val) {
			for(var i = 0; i < $scope.files.length; i++) {
				if($scope.files[i].check == true) {
					FileSystem.uploadFile($scope.files[i].name, $scope.order.order_id, $scope.order.order_type_id, function(data) {
						if(data.success == true) {
							$scope.file_id = data.result.file_id;
							$scope.order.activity_file = data.result.local_name;
							$scope.agentModal.hide();
						}
						GoDialog.alert('提示', data.msg);
					}, function(erro) {
						GoDialog.alert('提示', '上传失败');
					});

				}
			}

		};
		$scope.file_download = function(file_id) {
			FileSystem.downloadFile(file_id, function(data) {
				if(data.success == true) {
					GoDialog.alert('下载提示', '下载成功！请在[上传问卷]中查看已下载的文件,并确保您已安装阅读软件。如需编辑，请在电脑上进行修改');
				} else
					GoDialog.alert('下载提示', data.msg);
			}, function(e) {
				GoDialog.alert('下载提示', '下载失败');
			})
		}
		
		//控制发票信息显示
		$scope.showInvoiceInfo=function(){
			if($scope.showinvoicexia){
				$scope.invoiceflag=!$scope.invoiceflag;
			}
		}


		//客服列表
		var customPopup;
		$scope.showCustomList = function(store_id) {
			Buyerpayment.contact_show(store_id, function(data) {
				if(data != null){
					$scope.customList = data;
				}else{
					$scope.customList = new Object();
				}
				customPopup = $ionicPopup.show({
					templateUrl: 'templates/modal/customList.html',
					title: '联系客服',
					scope: $scope, //绑定数据，必须写这行
					buttons: [{
						text: "取消",
						type: "button-assertive"
					}]
				});
			})
		}
		
		//联系客服
		$scope.do_tel = function(param) {
			customPopup.close();　　　　
			if(param.type == 2) {
				$window.location.href = "tel:" + param.value;
			} else if(param.type == 1) {
				if(param.isonline == '0') {
					GoPage.go_txcall3(param.id, param.code, param.vvckey);
				} else {
					GoPage.go_txcall2($scope.store_id, $scope.store_id, param.id, param.code, param.vvckey);
				}
			}
		}
		
		

	}

	controller.$inject = ['$scope', 'Buyerpayment', 'GoPage', '$stateParams', '$ionicPopup', 'GoDialog', '$ionicModal', 'FileSystem','$timeout'];

	return app.registerController("buyerpaymentctrl", controller);
});